<?php
class HtmlUtil {
	public static function getCheckbox($name, $value, $checked, $attrs){
		$check = $checked ? 'checked' : '';
		
		$items = array();
		
		if(is_array($attrs) && count($attrs) > 0){
			foreach($attrs as $key => $val){
				$items[] = "{$key}=\"$val\"";
			}
		}
		
		$vl = join(" ", $items);
		
		return "<input type=\"checkbox\" name={$name} value=\"{$value}\" {$check} {$vl} >";
	}
	
	public static function getComboBox($name, $collection, $valueFunction, $displayFunction, $selectedValue){
		$arr = array();
		
		$arr[] = "<select name=\"{$name}\">";
		
		foreach($collection as $current){
			$value = $current->$valueFunction();
			$display = $current->$displayFunction();

			$selected = '';
			if($selectedValue == $value){
				$selected = ' selected ';
			}
			
			$arr[] = "<option value=\"{$value}\" {$selected}  >{$display}</option>";
		}
		
		$arr[] = "</select>";
		
		return join(' ', $arr);
	}
	
	public static function getImageUrl($id, $prefix){
		$res = null;
		
		// prefix can be reg or small
		if($prefix == 'reg' || $prefix == 'small'){
			$file = PictureConfig::$CONTENT_FOLDER . 'content/' . $prefix . '_' . $id . '.dat';
			$dummy = PictureConfig::$CONTENT_FOLDER . 'filler/' . $prefix . '_filler.jpg';
			
			if(file_exists($file)){
				$res = $file; 
			}
			else if(file_exists($dummy)){
				$res = $dummy;
			}
			else {
				throw new Exception('Both the file and the filler cannot be found');
			}
		}
		else {
			throw new Exception('The prefix for the image url must be either reg or small not ' . $prefix);
		}
		
		return $res; 
	}
	
	public static function hasMessages(){
		return HtmlUtil::hasInfo("messages.key");		
	}
	
	public static function getMessages(){
		return HtmlUtil::getInfo("messages.key"); 
	}
	
	public static function hasErrors(){
		return HtmlUtil::hasInfo("errors.key");
	}
	
	public static function getErrors(){
		return HtmlUtil::getInfo("errors.key");
	}
	
	public static function hasInfo($key){
		$res = false; 
		
		if(array_key_exists($key, $_SESSION)){
			$info = $_SESSION[$key];
			
			if(is_array($info) && count($info) > 0){
				$res = true;
			}
		}
		
		return $res;
	}
	
	public static function getInfo($key){
		$res = null;
		
		if(array_key_exists($key, $_SESSION)){
			$obj = $_SESSION[$key];
			
			// if the object at the specified location is array, will get all the elements, 
			// htmlspecialchars them and eventually glue them with br html element
			if(is_array($obj)){
				$rt = array();
				
				foreach($obj as $current){
					$rt[] = htmlspecialchars($current); 					
				}
				
				$res = join('<br/>', $rt);
			}
			
			// ok, now delete it
			unset($_SESSION[$key]);
		}
		
		return $res;
	}
	
	public static function addMessage($message){
		HtmlUtil::addInfo("messages.key", $message);
	}
	
	public static function addError($error){
		HtmlUtil::addInfo("errors.key", $error);
	}
	
	public static function addInfo($key, $message){
		// get the array in the session
		$arr = null;
		
		if(array_key_exists($key, $_SESSION)){
			$arr = $_SESSION[$key];
		}
		else {
			$arr = array();
		}
		
		$arr[] = $message;
		$_SESSION[$key] = $arr; 
	}
	
	public static function getSessionUser(){
		$res = null;
		$key = 'session.user';
		
		if(array_key_exists($key, $_SESSION)){
			$res = $_SESSION[$key];
		} 
		else {
			$res = new SessionUser();
			 
			$_SESSION[$key] = $res;
		}
		
		return $res;
	}
	
	function getRegularCombo($name, $list, $value){
		$arr = array();
		
		$arr[] = "<select name=\"{$name}\">";
		
		foreach($list as $current){
			$selected = $current == $value ? "selected" : "";
			
			$arr[] = "<option value=\"{$current}\"  {$selected} >{$current}</option>"; 
		}
		
		$arr[] = "</select>";
				
		return join(' ', $arr);
	}
} 
?>